package cn.blue.base.controller;

import cn.blue.base.config.BussinessException;
import cn.blue.base.service.LoginService;
import cn.blue.base.util.AjaxResult;
import cn.blue.base.vo.LoginVo;
import cn.blue.base.vo.WeChatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private LoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginVo loginVo){
        try {
            //调用业务层实现登录 返回值是token + logininfo
            Map<String,Object>map = loginService.accout(loginVo);
            return AjaxResult.me().setSuccess(true).setResultObj(map);
        } catch (BussinessException e) {
            //如果是自定义异常 那么返回的提示语就是service设置的提示语
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }
        catch (Exception e) {
            //如果是自定义异常 那么返回的提示语就是service设置的提示语
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统繁忙 请重试！！");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult weChat(@RequestBody WeChatVo weChatVo){
        try {
            return loginService.weChat(weChatVo);
        } catch (BussinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("网络繁忙！请稍后重试");
        }
    }


    /**
     * 绑定页面
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody WeChatVo vo){
        Map<String, Object> map = loginService.wechatBinder(vo);
        return AjaxResult.me().success(map);
    }
}
